package com.goldcn.picture.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.goldcn.common.CommonTools;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.common.util.CommonUtils;
import com.goldcn.common.util.IdGen;
import com.goldcn.enumeration.ErrorCodeEnum;
import com.goldcn.goods.dto.PageInfo;
import com.goldcn.picture.dao.PicLabelDetailMapper;
import com.goldcn.picture.dao.PicLabelMapper;
import com.goldcn.picture.dao.PicSpecialDetailMapper;
import com.goldcn.picture.dto.PicLabelDto;
import com.goldcn.picture.dto.QueryDetailDto;
import com.goldcn.picture.model.PicLabel;
import com.goldcn.picture.model.PicLabelDetail;
import com.goldcn.picture.vo.PicLabelDetailVo;
import com.goldcn.picture.vo.PicLabelVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class PicLabelService {

    @Autowired
    PicLabelMapper picLabelMapper;

    @Autowired
    PicLabelDetailMapper picLabelDetailMapper;


    /**
     * 添加图片标签
     * @param picLabelVo
     * @return
     */
    public Integer addPicLabel(PicLabelVo picLabelVo){
        PicLabel picLabel=new PicLabel();
        picLabel.setLabelid(IdGen.get().nextId());
        picLabel.setAddtime(CommonUtils.getCurTimestamp());
        picLabel.setLabelname(picLabelVo.getLabelname());
        picLabel.setLabeldes(picLabelVo.getLabeldes());
        picLabel.setStatus(picLabelVo.getStatus());
        picLabel.setUserid(picLabelVo.getUserid());
        picLabel.setSeq(picLabelVo.getSeq());

        Integer result =  picLabelMapper.insertSelective(picLabel);
        if(result != 1){
            throw CommonTools.createException(ErrorCodeEnum.addLabelFailure);
        }
        int index=0;
        if (picLabelVo.getPicturte().size()>0){
            for (String pic:picLabelVo.getPicturte()){
                PicLabelDetail detail=new PicLabelDetail();
                detail.setLabelid(picLabel.getLabelid());
                detail.setPictureid(Long.parseLong(pic));
                detail.setLabeldetailid(IdGen.get().nextId());
                detail.setAddtime(CommonUtils.getCurTimestamp());
                detail.setLabelname(picLabel.getLabelname());
                detail.setSeq(index);
                picLabelDetailMapper.insertSelective(detail);
                index++;
            }
        }

        return result;
    }


    /**
     * 更新图片标签
     * @param picLabelVo
     * @return
     */
    public Integer updateByPicLabel(PicLabelVo picLabelVo){

        PicLabel picLabel=new PicLabel();
        BeanUtils.copyProperties(picLabelVo,picLabel);

        Integer result =  picLabelMapper.updateByPrimaryKeySelective(picLabel);
        if(result != 1){
            throw CommonTools.createException(ErrorCodeEnum.addLabelFailure);
        }
        //picLabelDetailMapper.deleteByLabelId(picLabelVo.getLabelid());
/*        int index=0;
        if (picLabelVo.getPicturte().size()>0){
            for (String pic:picLabelVo.getPicturte()){
                PicLabelDetail detail=new PicLabelDetail();
                detail.setLabelid(picLabelVo.getLabelid());
                detail.setPictureid(Long.parseLong(pic));
                detail.setLabeldetailid(IdGen.get().nextId());
                detail.setAddtime(CommonUtils.getCurTimestamp());
                detail.setLabelname(picLabelVo.getLabelname());
                detail.setSeq(index);
                picLabelDetailMapper.insertSelective(detail);
                index++;
            }
        }*/

        return result;
    }


    /**
     * 通过labelID删除指定的label
     * @param labelID
     * @return
     */
    public Integer deleteByPrimaryKey(Long labelID){
        //判断标签是否使用
        if(picLabelMapper.labelUseCount(labelID)>0){
            throw CommonTools.createException(ErrorCodeEnum.lableIsUsed);
        }
        Integer result = picLabelMapper.deleteByPrimaryKey(labelID);
        if(result!=1){
            //抛出异常
            throw CommonTools.createException(ErrorCodeEnum.deleteLabelFailure);
        }
        return result;
    }

    /**
     * 根据ID查询标签
     * @param labelID
     * @return
     */
    public PicLabel selectByPrimaryKey(Long labelID){
        return picLabelMapper.selectByPrimaryKey(labelID);
    }

    /**
     * 根据图片ID查找标签
     * @param picId 图片ID
     * @return
     */
    public Object getPicLableList(Long picId,Integer pageNum, Integer pageSize){
        //String orderBy = "addTime";
        //String orderType = "desc";
        PageHelper.startPage(pageNum, pageSize);//分页初始化
        Page page = (Page) picLabelMapper.getPicLableList(picId);

        return new PageInfo(page, page.getPages(), page.getTotal());

    }

    /**
     * 根据用户ID查找标签
     * @param userId 图片ID
     * @return
     */
    public Object getUserPicLableList(Long userId,Integer pageNum, Integer pageSize){
        //String orderBy = "addTime";
        //String orderType = "desc";
        PageHelper.startPage(pageNum, pageSize);//分页初始化
        Page page = (Page) picLabelMapper.getUserPicLableList(userId);

        return new PageInfo(page, page.getPages(), page.getTotal());

    }

    /**
     * 根据标签ID删除当下所有所属图片
     * @param labelid
     * @return
     */
    public Object deletePicByLabelId(Long labelid){
        return picLabelDetailMapper.deleteByLabelId(labelid);
    }

    /**
     * 根据标签ID查询图片
     * @param labelid
     * @return
     */
    public Object queryPicInfoBylabelId(Long labelid,Integer pageNum, Integer pageSize){
        String orderBy = "addTime";
        String orderType = "desc";
        String orderStr = "label." + orderBy + " " + orderType;
        PageHelper.startPage(pageNum, pageSize,orderStr);//分页初始化
        Page page = (Page) picLabelMapper.queryPicInfoBylabelId(labelid);

        return new PageInfo(page, page.getPages(), page.getTotal());

    }

    public Object addPicLabelDetail(PicLabelDetailVo picLabelDetailVo){
        try {
            Integer result=0;
            int index=0;
            if (picLabelDetailVo.getPicturte().size()>0){
                for (String picId:picLabelDetailVo.getPicturte()){
                    int count=picLabelDetailMapper.selectByPicDetail(new QueryDetailDto(picLabelDetailVo.getLabelid(),Long.parseLong(picId)));
                    if (count >0) {
                        continue;
                    }
                    PicLabelDetail detail=new PicLabelDetail();
                    detail.setAddtime(CommonUtils.getCurTimestamp());
                    detail.setLabeldetailid(IdGen.get().nextId());
                    detail.setLabelname(picLabelDetailVo.getLabelname());
                    detail.setPictureid(Long.parseLong(picId));
                    detail.setLabelid(picLabelDetailVo.getLabelid());
                    detail.setSeq(index);
                    picLabelDetailMapper.insertSelective(detail);
                    index++;
                }
            }

            return result;

        }catch (ServiceException se) {
            throw se;
        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage());
            throw CommonTools.createException(ErrorCodeEnum.addSpecialDetailFailure);
        }
    }

    /**
     * 批量删除标签所属图片
     * @param detailIds
     */
    public Object delPicLabelDetail(List<String> detailIds){
        try {
            int result=0;
            if (detailIds.size()>0){
                for (String id:detailIds){
                    if (id!=null && !id.equals("")){
                        result=picLabelDetailMapper.deleteByPrimaryKey(Long.parseLong(id));
                    }
                }
            }
            return result;
        }catch (ServiceException se) {
            throw se;
        } catch (Exception e) {
            log.info(e.getMessage());
            throw CommonTools.createException(ErrorCodeEnum.operateOtherExceptionFailure);
        }
    }


}
